﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace AQMProject
{
    /// <summary>
    /// Interaktionslogik für MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {

        public class Url
        {
            public Uri url { get; set; }
        }
        
        public class DataObject
        {
            public String A { get; set; }
            public String B { get; set; }
            public String C { get; set; }
            public String D { get; set; }
            public String E { get; set; }
            public String F { get; set; }
            public String G { get; set; }
            public String H { get; set; }
        }
          
        private DateTime startDate;
        private DateTime endDate;

        private String symbol;
        private String bank;

        private Uri url;

        public MainWindow()
        {
            InitializeComponent();
                        
            InitForm();

            ShowColumnChart();
            ShowRssTable();
        }


        private void InitForm()
        {
            //Startdatum füllen.
            //this.startDate = new DateTime(2011, 10, 1, 0, 0, 0);
            this.startDate = new DateTime(2013, 1, 1, 0, 0, 0);
            this.startdate.SelectedDate = this.startDate;

            //Enddatum füllen.
            //this.endDate = new DateTime(2011, 12, 31, 0, 0, 0);
            this.endDate = new DateTime(2013, 3, 23, 0, 0, 0);
            this.enddate.SelectedDate = this.endDate;

            //Aktie füllen.
            //ComboBoxAktie füllen.
            AktieManager aktiemanager = new AktieManager();
            List<aktie> aktielist = aktiemanager.GetAktienList();

            foreach (aktie aktieentry in aktielist)
            {
                this.ComboBoxAktien.Items.Add(aktieentry.aktienname);
            }

            if (aktielist.Count > 0)
            {
                this.symbol = aktielist[0].aktienkuerzel;
            }

            this.ComboBoxAktien.SelectedIndex = 0;

            //Bank füllen.
            //ComboBoxBank füllen.
            BankManager bankmanager = new BankManager();
            List<bank> banklist = bankmanager.getBankList();

            foreach (bank bankentry in banklist)
            {
                this.ComboBoxBank.Items.Add(bankentry.bankname);
            }

            this.ComboBoxBank.SelectedIndex = 0;

        }

        //Tabelle erstellen
        private void ShowRssTable()
        {
            RssManager rssmanager = new RssManager();
            BankManager bankmanager = new BankManager();
            AktieManager aktienmanager = new AktieManager();

            List<rssuebersicht> rssuebersicht_liste = rssmanager.GetFiltertItems(this.startDate, this.endDate, this.symbol, bankmanager.getBankID(this.bank), false);
            var list = new ObservableCollection<DataObject>();

            foreach (rssuebersicht rssuebersicht in rssuebersicht_liste)
            {
                list.Add(new DataObject() { A = rssuebersicht.DatumUhrzeit.ToShortDateString(),
                                            B = rssuebersicht.DatumUhrzeit.ToShortTimeString(),
                                            C = bankmanager.getBankName(rssuebersicht.bank_idbank),
                                            D = rssmanager.getEmpfehlung(rssuebersicht.empfehlung_idempfehlung),
                                            E = rssuebersicht.nachricht,
                                            F = aktienmanager.GetAktienname(rssuebersicht.aktie_aktienkuerzel),
                                            G = rssuebersicht.aktie_aktienkuerzel,
                                            H = rssuebersicht.link
                                          });
            }

            this.dataGrid1.ItemsSource = list;
        }

        //Aktien Chart erstellen
        private void ShowColumnChart()
        {
            this.url = YahooUrlGenerator.createURL(this.symbol, this.startDate, this.endDate);

            base.DataContext = new Url
            {
                url = YahooUrlGenerator.createURL(symbol, this.startDate, this.endDate)
            };

            List<ShareData> kurse = YahooCsvLoader.readYahhoCsvFromWeb(url);

            List<KeyValuePair<DateTime, float>> valueList = new List<KeyValuePair<DateTime, float>>();

            foreach (ShareData kurs in kurse)
            {
                valueList.Add(new KeyValuePair<DateTime, float>(kurs.Date, kurs.Close));
            }
            
            lineChart.DataContext = valueList;
        }


        //Formular aktualisieren
        private void Refresh_Click(object sender, RoutedEventArgs e)
        {
            ShowColumnChart();
            ShowRssTable();
        }


        //Werte Änderungen abfangen

        private void ComboBoxAktien_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            AktieManager aktiemanager = new AktieManager();
            this.symbol = aktiemanager.GetAktienkuerzel(this.ComboBoxAktien.SelectedItem.ToString());
        }

        private void ComboBoxBank_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            this.bank = this.ComboBoxBank.SelectedItem.ToString();
        }

        private void startdate_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            this.startDate = Convert.ToDateTime(sender.ToString());
        }

        private void enddate_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            this.endDate = Convert.ToDateTime(sender.ToString());
        }


        //Buttons zu anderen Formularen

        private void Berechnung_Click(object sender, RoutedEventArgs e)
        {
            checkBerechnung w = new checkBerechnung();
            w.setParameter(this.startDate, this.endDate, this.symbol, this.bank);
            w.InitForm();
            w.Show();
        }

        private void Vergleichen_Click(object sender, RoutedEventArgs e)
        {
            Vergleichen w = new Vergleichen();
            w.setParameter(this.startDate, this.endDate, this.symbol, this.bank);
            w.InitForm();
            w.Show();
        }

        private void RssVerwaltung_Click(object sender, RoutedEventArgs e)
        {
            Window w = new Einrichtung();
            w.Show();

            //WriteRSSinDB writeRss = new WriteRSSinDB();
            //writeRss.PseudoMain();
        }

        private void Window_Closing_1(object sender, System.ComponentModel.CancelEventArgs e)
        {

        }

    }

    
}

